package com.unicloud.medical.oss.common.util;

import com.unicloud.medical.oss.common.enums.ErrorEnum;
import com.unicloud.medical.oss.common.model.DownloadObject;
import com.unicloud.medical.oss.common.model.FileMetaData;
import com.unisound.medical.common.exception.BusinessException;
import com.unisound.medical.common.util.AssertUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FileUtil {

    /**
     * 绝对路径文件名转文件名
     * @param absolutePathFileName
     */
    public static String getFileName(String absolutePathFileName) {
        if (StringUtils.isEmpty(absolutePathFileName)) return null;
        int i = absolutePathFileName.lastIndexOf(File.separator);
        if (i < 0) return null;
        return absolutePathFileName.substring(i + 1);
    }

    /**
     * 从objectName中查找文件名
     * @param objectName
     */
    public static String findFileName(String objectName) {
        if (StringUtils.isEmpty(objectName)) return null;
        int i = objectName.lastIndexOf("/");
        if (i < 0) return objectName;
        return objectName.substring(i + 1);
    }

    /**
     * 获取文件名
     *
     * @param file
     * @return
     */
    public static String getFileName(MultipartFile file) {
        if (Objects.isNull(file)) return null;
        return file.getOriginalFilename();
    }

    public static boolean validFile(File file) {
        return file != null && file.exists() && !file.isDirectory();
    }

    public static FileMetaData buildFileMetaData(MultipartFile file) {
        try {
            return new FileMetaData(file.getBytes(), file.getContentType(), file.getSize());
        } catch (Exception e) {
            throw new BusinessException(ErrorEnum.FILE_NOT_EXIST);
        }
    }

    public static ResponseEntity<byte[]> download(DownloadObject downloadObject) {
        if (Objects.isNull(downloadObject)) return null;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", downloadObject.getContentType());
        headers.add("Content-Disposition", "attachment; filename=" + downloadObject.getFileName());
        return ResponseEntity.ok().headers(headers).body(downloadObject.getData());
    }

    public static FileMetaData multipartFileToMeta(MultipartFile file) {
        try {
            FileMetaData fileMetaData = new FileMetaData(file.getBytes(), file.getContentType(), file.getSize(), file.getOriginalFilename());
            AssertUtil.assertTrue(Objects.nonNull(file) && Objects.nonNull(file.getInputStream()) && StringUtils.isNotEmpty(file.getContentType()), ErrorEnum.FILE_NOT_EXIST);
            AssertUtil.assertTrue(fileMetaData.getSize() > 0, ErrorEnum.FILE_CONTENT_NOT_EXIST);
            return fileMetaData;
        } catch (Exception e) {
            throw new BusinessException(ErrorEnum.FILE_NOT_EXIST);
        }
    }

    public static List<FileMetaData> multipartFilesToMetas(List<MultipartFile> files) {
        AssertUtil.assertTrue(CollectionUtils.isNotEmpty(files), ErrorEnum.FILES_INVALID);
        List<FileMetaData> fileMetaDataList = new ArrayList<>();
        for (MultipartFile file : files) {
            fileMetaDataList.add(multipartFileToMeta(file));
        }
        return fileMetaDataList;
    }

}
